//
//  UITableViewController+HDOtherUserInfo.m
//  app
//
//  Created by D on 15/4/16.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDOtherUserInfo.h"
#import "HDUserInforHttpRequestManger.h"

@interface HDOtherUserInfo ()

@property (strong,nonatomic) JKRemoteResult * result_otherinfo;
@property (strong,nonatomic) NSString * discuz_id;
@property (strong,nonatomic) NSString * topic_id;

@end

@implementation  HDOtherUserInfo 

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self refreshOtherInfo];
    
//    [self loadOtherInfo];
}

- (void)viewDidLoad {
    [super viewDidLoad];
  

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)refreshOtherInfo
{
    if (self.fuid.integerValue - [[[NSUserDefaults standardUserDefaults]objectForKey:@"otheruid"] integerValue] != 0) {
        
        self.progress_ = [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:self.progress_];
        [self.view bringSubviewToFront:self.progress_];
        self.progress_.delegate = self;
        self.progress_.labelText = @"请稍后...";
        [self.progress_ show:YES];
        
        
        self.result_otherinfo = [JKRemoteResult target:self action:@selector(getuserOtherInfo:)];
        HDUserInforHttpRequestManger * httpManager = [HDUserInforHttpRequestManger shareUserInforHttpRequestManger];
        NSDictionary * params = @{@"fuid":self.fuid};
        [httpManager getUserInfoWithParams:params result:self.result_otherinfo];
    }else
    {
        [self loadOtherInfo];
    }

 
}
-(void)getuserOtherInfo:(JKRemoteResult*)rt
{
    if (self.progress_) {
        [self.progress_ removeFromSuperview];
        self.progress_ = nil;
    }
    
    if (rt.failure) {
        
        UIAlertView * av = [[UIAlertView alloc]initWithTitle:nil message:@"请检查网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [self.view addSubview:av];
        [av show];
        
        return;
    }

    if ([[[rt.content objectForKey:@"user_info"] objectForKey:@"nlv_icon"]length] == 0) {
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"othernlv_icon"];
    }else
    {
        [[NSUserDefaults standardUserDefaults] setObject:[[rt.content objectForKey:@"user_info"] objectForKey:@"nlv_icon"] forKey:@"othernlv_icon"];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:[[rt.content objectForKey:@"user_info"] objectForKey:@"regtime"] forKey:@"otherUserTime"];
    [[NSUserDefaults standardUserDefaults] setObject:[[rt.content objectForKey:@"user_info"] objectForKey:@"uid"] forKey:@"otheruid"];
    
     [[NSUserDefaults standardUserDefaults] setObject:[[rt.content objectForKey:@"user_info"] objectForKey:@"signature"] forKey:@"othersignature"];
    [[NSUserDefaults standardUserDefaults] setObject:[[rt.content objectForKey:@"user_info"] objectForKey:@"nickname"] forKey:@"othernickname"];
    [[NSUserDefaults standardUserDefaults] setObject:[[rt.content objectForKey:@"user_info"] objectForKey:@"gender"] forKey:@"othergender"];
    [[NSUserDefaults standardUserDefaults] setObject:[[rt.content objectForKey:@"user_info"] objectForKey:@"face"] forKey:@"otherface"];
    [[NSUserDefaults standardUserDefaults] setObject:[[rt.content objectForKey:@"user_info"] objectForKey:@"level"] forKey:@"otherlevel"];
    [[NSUserDefaults standardUserDefaults] setObject:[[rt.content objectForKey:@"user_info"] objectForKey:@"level_icon"] forKey:@"otherlevel_icon"];
    [[NSUserDefaults standardUserDefaults] setObject:[[rt.content objectForKey:@"user_info"] objectForKey:@"nlv"] forKey:@"othernlv"];
    
    [[NSUserDefaults standardUserDefaults] setObject:[[rt.content objectForKey:@"user_info"] objectForKey:@"uniqueID"] forKey:@"otheruniqueID"];
//  个人动态为
    [[NSUserDefaults standardUserDefaults] setObject:[[rt.content objectForKey:@"user_info"] objectForKey:@"last_topic"] forKey:@"otherstate"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
    
    [self loadOtherInfo];
    
}
-(void)loadOtherInfo
{
    
    UIImage * BGimage = [UIImage imageNamed:@"head_144.png"];
    CGFloat top = 6;
    CGFloat bottom = 6;
    CGFloat left = 6;
    CGFloat right = 6;
    UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
    BGimage = [BGimage resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    [self.line_image setImage:BGimage];
    
    self.user_name.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"othernickname"];
    self.darenhao.text = [NSString stringWithFormat:@"达人号:%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"otheruniqueID"]];

    [self.user_iamge sd_setImageWithURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"otherface"]]];
    
    
    
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"othergender"]integerValue] == 0) {
        [self.gender_image setImage:[UIImage imageNamed:@"girl.png"]];
    }else
    {
        [self.gender_image setImage:[UIImage imageNamed:@"boy.png"]];
    }
    
    
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"othernlv_icon"]length] > 1) {
        self.levelLab.text = [NSString stringWithFormat:@"认证营养师LV%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"othernlv"]];
        [self.level_image setImage:[UIImage imageNamed:@"forum_list_yys"]];
        
    }else
    {
        
        self.levelLab.text = [NSString stringWithFormat:@"LV%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"otherlevel"]];
        [self.level_image sd_setImageWithURL:[[NSUserDefaults standardUserDefaults]objectForKey:@"otherlevel_icon"]];
    }
    
    
    UIFont * font = [UIFont systemFontOfSize:14];
    self.sign_new.lineBreakMode = NSLineBreakByCharWrapping;
    self.sign_new.numberOfLines = 2;
    [self.sign_new setFont:font];
 
    
    self.sign_new.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"othersignature"];
    
    
    self.user_iamge.layer.masksToBounds = YES;
    self.user_iamge.layer.cornerRadius = UF(6); //UF(6)为圆角角度
    self.line_image.layer.masksToBounds = YES;
    self.line_image.layer.cornerRadius = UF(6);
    
    //    状态内容
    [self userState];

    
}
#pragma mark - 状态内容
-(void)userState
{


    
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"otherstate"]count] > 0) {
        self.state.text = [[[NSUserDefaults standardUserDefaults]objectForKey:@"otherstate"] objectForKey:@"title"];
        self.discuz_id = [[[NSUserDefaults standardUserDefaults]objectForKey:@"otherstate"]objectForKey:@"discuz_id"];
        self.topic_id = [[[NSUserDefaults standardUserDefaults]objectForKey:@"otherstate"]objectForKey:@"topic_id"];
        
        
    }else
    {
        self.state.text = @"";
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSUInteger section = [indexPath section];
    switch (section) {
        case 0:
        {}
            break;
            
        case 1:
        {
            if (indexPath.row == 1) {
                
                if (self.state.text.length > 0) {
                    
//                    [[tableView cellForRowAtIndexPath:indexPath] setSelectionStyle:UITableViewCellSelectionStyleDefault];
//                    
//                    NSDictionary *params = @{@"discuz_id":self.discuz_id ,@"topic_id":self.topic_id};
//                    HZForumDetialViewController *forumVC = [[HZForumDetialViewController alloc] initWithRequestParams:params];
//                    forumVC.hidesBottomBarWhenPushed = YES;
//                    [self.navigationController pushViewController:forumVC animated:YES];
                }else
                {
                     [[tableView cellForRowAtIndexPath:indexPath] setSelectionStyle:UITableViewCellSelectionStyleNone];
                }
                
               
            }
        }
            break;
            
        default:
            break;
    }
    
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [alertView removeFromSuperview];
}

@end














